package com.yingjia.yingjiauniversity.http.datasource;

import com.google.gson.JsonObject;
import com.yingjia.yingjiauniversity.bean.base.SignatureEntity;
import com.yingjia.yingjiauniversity.bean.http.HttpAddress;
import com.yingjia.yingjiauniversity.bean.http.ObtainSignature;
import com.yingjia.yingjiauniversity.http.exception.ApiServerResultFunction;
import com.yingjia.yingjiauniversity.http.exception.ResponseFunc;
import com.yingjia.yingjiauniversity.retrofit.request.RequestBuilder;
import com.yingjia.yingjiauniversity.service.SignatureService;

import io.reactivex.Observable;

/**
 * 远程服务器数据源
 *
 * @author mars.yu
 * @date 2018 /5/22
 */
public class SignatureRemoteDataSourceImpl implements SignatureDataSource {

    @Override
    public Observable<SignatureEntity<ObtainSignature>> getSignatureInfo(String orderNo) {

        //TODO 为了快速开发，在接口完成之前创建一个本地的 .json文件，直接读取然后 new Gson().fromJson(),强转为我们需要的bean
        ////TODO 使用本地json数据调试不必等待接口完成，如此实现并行开发
        //String json = AssetUtils.readFile2String(App.context(), "signature_upload.json");
        //SignatureEntity<ObtainSignature> entity = (SignatureEntity<ObtainSignature>) new Gson()
        //    .fromJson(json, new TypeToken<SignatureEntity<ObtainSignature>>() {}.getType());
        //return Observable.just(entity);

        //TODO 源码实现网络请求
        final JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("orderNo", orderNo);

        return new RequestBuilder()
                .url(HttpAddress.URL_IP)//baseUrl
                .json(SignatureService.class).getSignatureInfo(jsonObject)
                .map(new ApiServerResultFunction<ObtainSignature>())
                .onErrorResumeNext(new ResponseFunc<SignatureEntity<ObtainSignature>>());
    }
}
